﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SaaS;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SaaS
{
//    Add, Edit, Delete, Execute

//    Add, Edit -> Show a page to change the attributes of the workflow (WorkflowEdit)
//    Delete -> Delet workflow and update list
//    Execute -> Show execution page. Enter initial value and final value is displayed (WorkflowExecute)
    public partial class WorkflowManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoginID"] == null)
                Response.Redirect("UserLogin.aspx");
            if (ddlSystemWorkflows.Items.Count <= 0)
            {
                string loginID = Session["LoginID"].ToString();
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                SqlDataReader reader;
                reader = db.executeQuery("SELECT * FROM SYSTEM_WORKFLOW");
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ddlSystemWorkflows.Items.Add(reader["System_Workflow_ID"].ToString() + ". " + reader["System_Workflow_Name"].ToString());
                    }
                }
                else
                {
                    txtCustomDescription.Enabled = true;
                    txtCustomName.Enabled = true;
                }
                ddlSystemWorkflows.Items.Add("Add Custom:");

                reader.Close();
                reader = db.executeQuery("SELECT * FROM TENANT_WORKFLOW WHERE Login_ID = " + loginID + ";");
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ddlTenantWorkflows.Items.Add(reader["Tenant_Workflow_ID"].ToString() + ". " + reader["Tenant_Workflow_Name"].ToString());
                    }
                }
                reader.Close();
                db.close();
            }
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSystemWorkflows_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSystemWorkflows.SelectedIndex == ddlSystemWorkflows.Items.Count - 1)
            {
                txtCustomDescription.Enabled = true;
                txtCustomName.Enabled = true;
            }
            else
            {
                txtCustomDescription.Enabled = false;
                txtCustomName.Enabled = false;
            }
        }

        protected void resetErrors()
        {
            lblErr.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            resetErrors();
            string customName = txtCustomName.Text.Trim();
            if (ddlSystemWorkflows.SelectedIndex == ddlSystemWorkflows.Items.Count - 1 &&
                customName.Length <= 0)
            {
                lblErr.Text = "Please provide workflow name";
                return;
            }
            Session["WorkflowMode"] = "New";
            if (ddlSystemWorkflows.SelectedIndex == ddlSystemWorkflows.Items.Count - 1)
            {
                Session["WorkflowID"] = -1;
                Session["WorkflowName"] = txtCustomName.Text;
                Session["WorkflowDescription"] = txtCustomDescription.Text;
            }
            else
            {
                Session["WorkflowID"] = ddlSystemWorkflows.Text.Substring(0, ddlSystemWorkflows.Text.IndexOf('.'));
                Session["WorkflowName"] = ddlSystemWorkflows.Text.Substring(ddlSystemWorkflows.Text.IndexOf('.')+2);
            }
            Response.Redirect("WorkflowEdit.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            resetErrors();
            Session["WorkflowMode"] = "Edit";
            Session["WorkflowID"] = ddlTenantWorkflows.Text.Substring(0, ddlTenantWorkflows.Text.IndexOf('.'));
            Session["WorkflowName"] = ddlTenantWorkflows.Text.Substring(ddlTenantWorkflows.Text.IndexOf('.') + 2);
            Response.Redirect("WorkflowEdit.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            resetErrors();
            Session["WorkflowID"] = ddlTenantWorkflows.Text.Substring(0, ddlTenantWorkflows.Text.IndexOf('.'));
            Session["WorkflowName"] = ddlTenantWorkflows.Text.Substring(ddlTenantWorkflows.Text.IndexOf('.') + 2);
            Response.Redirect("WorkflowExecute.aspx");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string workflowID = ddlTenantWorkflows.Text.Substring(0, ddlTenantWorkflows.Text.IndexOf('.'));
            MiniSaaSDB db = new MiniSaaSDB();
            db.open();
            db.executeNonQuery("DELETE FROM TENANT_WORKFLOW_SERVICE WHERE Tenant_Workflow_ID = " + workflowID);
            db.executeNonQuery("DELETE FROM TENANT_WORKFLOW WHERE Tenant_Workflow_ID = " + workflowID);
            db.close();
            ddlTenantWorkflows.Items.RemoveAt(ddlTenantWorkflows.SelectedIndex);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);
                string serviceid = GridView1.DataKeys[index].Value.ToString();
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                db.executeNonQuery("DELETE FROM SYSTEM_WORKFLOW_SERVICE WHERE Service_ID = " + serviceid + ";");
                db.executeNonQuery("DELETE FROM TENANT_WORKFLOW_SERVICE WHERE Service_ID = " + serviceid + ";");
                db.executeNonQuery("DELETE FROM SERVICE WHERE Service_ID = " + serviceid + ";");
                db.close();
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
        {

        }
    }
}
